<?php 
class AdAdmin
{
	/*
	public function AdAdmin()
	{
		self::runPreActions();
		self::runPostActions();
	}
	*/
	public function runPreActions()
	{
		global $commander;
		global $adc;
		switch($commander->getPostAction())
		{	
			case 'list_ad':
				if($_POST['new_item_Title'] != '')
				{ 
					self::addAd($_POST['new_item_Title'], $_POST['new_item_TitleEn'], intval($_POST['new_item_TypeId']), intval($_POST['new_item_CatId']), intval($_POST['new_item_BrandId']), intval($_POST['new_item_IsBuying']), intval($_POST['new_item_IsNew']));
				}
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'am_ads', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'am_ads', 0); }
				if($_POST['archive'] > 0)
				{ $commander->setTableFieldValueOnId('am_ads', 'archived', 1, intval($_POST['archive'])); }
				if($_POST['unarchive'] > 0)
				{ $commander->setTableFieldValueOnId('am_ads', 'archived', 0, intval($_POST['unarchive'])); }
				if($_POST['del_item_id'] > 0)
				{ $commander->setTableFieldValueOnId('am_ads', 'deleted', 1, intval($_POST['del_item_id'])); }
				if(intval($_POST['restore']) > 0)
				{ $commander->setTableFieldValueOnId('am_ads', 'deleted', 0, intval($_POST['restore'])); }
				return;
			break;
		}
	}
	
	public function runPostActions()
	{
		global $db;
		global $commander;
		global $adc;
		
		switch ($_POST['action'])
		{
			case 'list_ad':
				if((count($_POST['item_Id']) > 0) && ($adc->getAds()))
				{
					$i = 0;
					foreach($_POST['item_Id'] as $id)
					{
						if(isset($adc->Ads[$id]))
						{
							if($_POST['item_TypeId'][$i] != $adc->Ads[$id]->getTypeId())
							{ 
								$adc->Ads[$id]->setTypeId($_POST['item_TypeId'][$i]);
								$adc->Ads[$id]->setDataChanged(true);
							}
							if($_POST['item_CatId'][$i] != $adc->Ads[$id]->getCatId())
							{ 
								$adc->Ads[$id]->setCatId($_POST['item_CatId'][$i]);
								$adc->Ads[$id]->setDataChanged(true);
							}
							if($_POST['item_BrandId'][$i] != $adc->Ads[$id]->getBrandId())
							{ 
								$adc->Ads[$id]->setBrandId($_POST['item_BrandId'][$i]);
								$adc->Ads[$id]->setDataChanged(true);
							}
							if($_POST['item_Title'][$i] !== $adc->Ads[$id]->getTitle())
							{ 
								$adc->Ads[$id]->setTitle($commander->formatText($_POST['item_Title'][$i]));
								$adc->Ads[$id]->setDataChanged(true);
							}
							if($_POST['item_TitleEn'][$i] !== $adc->Ads[$id]->getTitleEn())
							{ 
								$adc->Ads[$id]->setTitleEn($commander->formatText($_POST['item_TitleEn'][$i]));
								$adc->Ads[$id]->setDataChanged(true);
							}
							if($_POST['item_IsBuying_'.$id] != $adc->Ads[$id]->getIsBuying())
							{ 
								$adc->Ads[$id]->setIsBuying(intval($_POST['item_IsBuying_'.$id]));
								$adc->Ads[$id]->setDataChanged(true);
							}
							if($_POST['item_IsNew_'.$id] != $adc->Ads[$id]->getIsNew())
							{ 
								$adc->Ads[$id]->setIsNew(intval($_POST['item_IsNew_'.$id]));
								$adc->Ads[$id]->setDataChanged(true);
							}
							if($_POST['item_IsCompleted_'.$id] != $adc->Ads[$id]->getIsCompleted())
							{ 
								$adc->Ads[$id]->setIsCompleted(intval($_POST['item_IsCompleted_'.$id]));
								$adc->Ads[$id]->setDataChanged(true);
							}
							$adc->Ads[$id]->update();
						}
						$i++;
					} //End foreach
					if($_POST['selected'])
					{
						foreach($_POST['selected'] as $id)
						{
							if(isset($adc->Ads[$id]))
							{
								if($_POST['activate'] == '-1')
								{
									$adc->Ads[$id]->setActive(1);
									$commander->setTableActive($id, 'am_ads', 1); 
								}
								if($_POST['deactivate'] == '-1')
								{
									$adc->Ads[$id]->setActive(0);
									$commander->setTableActive($id, 'am_ads', 0); 
								}
							}
						}
					}
				}
			break;
			//List-Editor group actions end
			
			//Editor Ad actions
			case 'editor_ad':
				if($adc->getCurrentAdId() > 0)
				{
					if(is_object($adc->getCurrentAd()))
					{
						$params = array();
						foreach(array_keys($_POST) as $key)
						{
							if($adc->varExsists($key, $adc->CurrentAd))
							{
								switch($key)
								{
									case 'Title':
									case 'TitleEn':
									case 'Description':
									case 'DescriptionEn':
									case 'ContactEmail':
									case 'ContactName':
									case 'ContactCompany':
									case 'ContactMob':
									case 'ContactPhone':
									case 'LocationAddress':
									case 'LocationZip':
									case 'LocationCity':
									case 'LocationCountry':
										if($commander->formatText($_POST[$key]) !== $adc->CurrentAd->__call('get'.$key))
										{
											$params[$key] = $commander->formatText($_POST[$key]);
											$adc->CurrentAd->setDataChanged(true);
										}
									break;
									case 'TypeId':
									case 'CatId':
									case 'BrandId':
										if(intval($_POST[$key]) !== $adc->CurrentAd->__call('get'.$key))
										{
											$params[$key] = intval($_POST[$key]);
											$adc->CurrentAd->setDataChanged(true);
										}
									break;
								}
							}
							if(count($params) > 0)
							{ $adc->CurrentAd->setValues($params); }
							$adc->CurrentAd->Update();
						}
						
						if($_FILES['Image'])
						{
							$file = $_FILES['Image'];
							if($file["name"] != '')
							{
								if($file['size'] < 1000000)
								{ $adc->CurrentAd->uploadImage($_FILES['Image']); }
								else
								{ $commander->ErrorField['Image'] = 'image file size exceded 1mb'; }
							}
							$adc->CurrentAd->setItemLoaded('images', false);
						}
						if(intval($_POST['del_img_id']) > 0)
						{ 
							$adc->CurrentAd->deleteImage(intval($_POST['del_img_id']));
							$adc->CurrentAd->setItemLoaded('images', false);
						}
						
						if($_POST['up'] > 0)
						{ 
							$commander->setTableItemOrderUp($_POST['up'], 'am_ad_images', 'imageOrder', 'adId');
							$adc->CurrentAd->setImages(array());
							$adc->CurrentAd->setItemLoaded('images', false);
						}
						if($_POST['down'] > 0)
						{ 
							$commander->setTableItemOrderDown($_POST['down'], 'am_ad_images', 'imageOrder', 'adId');
							$adc->CurrentAd->setImages(array());
							$adc->CurrentAd->setItemLoaded('images', false);
						}
					}
				}
			break;
		}
	}
	
	function addAd($title, $titleEn, $typeId=0, $catId=0, $brandId=0, $isBuying=0, $isNew=0)
	{
		global $commander;
		global $user;
		
		if(is_object($user))
		{ $userId = $user->getId(); }
		else
		{ $userId = 1; }
		
		$title = $commander->formatText($title);
		$titleEn = $commander->formatText($titleEn);
		
		if($title != '')
		{
			$sql  = 'INSERT INTO '.DB_PERFIX.'am_ads (`title`, `titleEn`, `typeId`, `catId`, `brandId`, `userId`, `isBuying`, `isNew`, `dateCreated`)'; 
			$sql .= 'VALUES ("'.$title.'", "'.$titleEn.'", '.intval($typeId).', '.intval($catId).', '.intval($brandId).', '.intval($userId).', '.intval($isBuying).', '.intval($isNew).', NOW());';
			$result = mysql_query($sql);
		}
	}
	
	function deleteAd($id)
	{	
		if($id > 0)
		{	
			$sql = 'DELETE FROM '.DB_PERFIX.'am_categories WHERE id='.intval($id).' LIMIT 1;';
			$result = mysql_query($sql);
			return;
		}
	}
}
?>